from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

# 指定chromedriver路径（137版本）
driver_path = "D:\\pycharm\\PM_test\\chromedriver.exe"

# 创建WebDriver实例
service = Service(driver_path)
options = Options()
browser = webdriver.Chrome(service=service, options=options)
browser.maximize_window()
wait = WebDriverWait(browser, 10)

# 登录函数
def login():
    browser.get("http://146.56.217.9:9131")
    browser.find_element(By.ID, "username").send_keys("admin_9131")
    browser.find_element(By.ID, "password").send_keys("admin")
    browser.find_element(By.CLASS_NAME, "button").click()

# 封装制造订单搜索函数
def order_search(search_data):
    # 跳转制造订单列表界面
    browser.find_element(By.ID, "mainmenua_mrp").click()
    browser.find_element(By.CSS_SELECTOR, ".menu_contenu_mrp_mo_list .vsmenu").click()

    # 编号
    browser.find_element(By.NAME, "search_ref").send_keys(search_data["ref"])
    # 物料清单
    browser.find_element(By.CSS_SELECTOR, f"select#search_fk_bom>option:nth-child({search_data['fk_bom']})").click()
    # 产品
    browser.find_element(By.CSS_SELECTOR, f"select#search_fk_product>option:nth-child({search_data['fk_product']})").click()
    # 生产数量
    browser.find_element(By.NAME, "search_qty").send_keys(str(search_data["qty"]))
    # 标签
    browser.find_element(By.NAME, "search_label").send_keys(search_data["label"])
    # 生产仓库
    browser.find_element(By.CSS_SELECTOR, f"select#search_fk_warehouse>option:nth-child({search_data['fk_warehouse']})").click()
    # 计划开始日期
    browser.find_element(By.NAME, "search_date_start_planned_dtstart").send_keys(search_data["date_start_planned_dtstart"])
    browser.find_element(By.NAME, "search_date_start_planned_dtend").send_keys(search_data["date_start_planned_dtend"])
    # 计划结束日期
    browser.find_element(By.NAME, "search_date_end_planned_dtstart").send_keys(search_data["date_end_planned_dtstart"])
    browser.find_element(By.NAME, "search_date_end_planned_dtend").send_keys(search_data["date_end_planned_dtend"])
    # 搜索按钮
    browser.find_element(By.NAME, "button_search_x").click()

if __name__ == '__main__':
    try:
        login()
        sleep(1)  # 等待页面加载

        # 测试数据
        # 制造订单搜索功能测试数据（3正向+3反向，共6个用例）
        # 测试结果：与预计全部一致
        test_data = [
            # --------------------- 正向测试用例 ---------------------
            {
                "ref": "MO-20250614-001",  # 假设存在的订单编号
                "fk_bom": 2,  # 手机主板BOM
                "fk_product": 2,  # 第二个产品
                "qty": 100,
                "label": "手机主板生产订单-20250614",
                "fk_warehouse": 2,  # 第二个仓库
                "date_start_planned_dtstart": "2025/06/14",
                "date_start_planned_dtend": "2025/06/14",
                "date_end_planned_dtstart": "2025/07/15",
                "date_end_planned_dtend": "2025/07/15",
                "expected_result": "搜索结果显示1条记录，与输入条件完全匹配"
            },
            {
                "ref": "",  # 不填写编号
                "fk_bom": 3,  # 电脑组装BOM
                "fk_product": 4,  # 第四个产品
                "qty": "",  # 不填写数量
                "label": "",  # 不填写标签
                "fk_warehouse": 3,  # 第三个仓库
                "date_start_planned_dtstart": "2025/06/01",
                "date_start_planned_dtend": "2025/06/30",
                "date_end_planned_dtstart": "2025/08/01",
                "date_end_planned_dtend": "2025/08/31",
                "expected_result": "搜索结果显示所有符合日期范围的电脑组装订单"
            },
            {
                "ref": "2025",  # 部分编号
                "fk_bom": 2,  # 手机主板BOM
                "fk_product": 2,  # 第二个产品
                "qty": 1,  # 最小数量
                "label": "样品",  # 部分标签
                "fk_warehouse": 2,  # 第二个仓库
                "date_start_planned_dtstart": "2025/06/01",
                "date_start_planned_dtend": "2025/06/30",
                "date_end_planned_dtstart": "2025/06/15",
                "date_end_planned_dtend": "2025/06/25",
                "expected_result": "搜索结果显示编号包含'2025'、标签包含'样品'的手机主板样品订单"
            },

            # --------------------- 反向测试用例 ---------------------

            {

                "ref": "",
                "fk_bom": 2,
                "fk_product": 2,
                "qty": "",
                "label": "",
                "fk_warehouse": 2,
                "date_start_planned_dtstart": "2026/01/01",  # 未来日期
                "date_start_planned_dtend": "2026/01/31",  # 未来日期
                "date_end_planned_dtstart": "2026/02/01",  # 未来日期
                "date_end_planned_dtend": "2026/02/28",  # 未来日期
                "expected_result": "搜索结果显示0条记录，提示'未找到匹配数据'"
            },
            {

                "ref": "",
                "fk_bom": 2,
                "fk_product": 2,
                "qty": "",
                "label": "",
                "fk_warehouse": 2,
                "date_start_planned_dtstart": "2025/07/15",  # 结束日期早于开始日期
                "date_start_planned_dtend": "2025/07/10",
                "date_end_planned_dtstart": "2025/08/30",
                "date_end_planned_dtend": "2025/08/25",
                "expected_result": "提示'开始日期不能晚于结束日期'，搜索框保持原有内容"
            },
            {
                "ref": "",
                "fk_bom": 99,  # 不存在的BOM
                "fk_product": 2,
                "qty": "",
                "label": "",
                "fk_warehouse": 2,
                "date_start_planned_dtstart": "2025/06/14",
                "date_start_planned_dtend": "2025/06/14",
                "date_end_planned_dtstart": "2025/07/15",
                "date_end_planned_dtend": "2025/07/15",
                "expected_result": "搜索结果显示0条记录，提示'未找到匹配数据'"
            },
        ]
        for data in test_data:
            order_search(data)
            sleep(1)  # 等待搜索结果加载
    except Exception as e:
        print("发生错误：", e)
    finally:
        sleep(1)
        browser.quit()